function [stru_grid,strubox_rigid,aerobox,stru_grid_up,stru_grid_down,stru_quad_up,stru_quad_down,stru_quad_up_plus,stru_quad_down_plus]=reading_data
%% 读取两个分卡片数据
fgrid=fopen('grid.bdf','r');
i=1;
stru_grid=zeros(0,4);
while feof(fgrid)==0
    entry=fgetl(fgrid);
    len=length(entry);
    stru_grid(i,1)=sscanf(entry(9:16),'%d');
    stru_grid(i,2)=sscanf(entry(25:32),'%f');
    stru_grid(i,3)=sscanf(entry(33:40),'%f');
    stru_grid(i,4)=sscanf(entry(41:len),'%f');
    i=i+1;
end
fclose(fgrid);

fquad=fopen('quad.bdf','r');
i=1;
stru_quad=zeros(0,6);
while feof(fquad)==0
    entry=fgetl(fquad);
    len=length(entry);
    stru_quad(i,:)=sscanf(entry(9:min(56,len)),'%d%d%d%d%d%d');
    i=i+1;
end
fclose(fquad);

%% 进行第一轮上下表面分类
stru_quad(:,7)=0;
stru_quad_undetermined=[];
stru_grid_up=[];
stru_grid_down=[];
stru_quad_up=[];
stru_quad_down=[];
stru_quad_plus=[];
i=1;
while i<=size(stru_quad,1)
    m=[stru_grid(stru_quad(i,4),2)-stru_grid(stru_quad(i,3),2),stru_grid(stru_quad(i,4),3)-stru_grid(stru_quad(i,3),3),stru_grid(stru_quad(i,4),4)-stru_grid(stru_quad(i,3),4)];
    n=[stru_grid(stru_quad(i,6),2)-stru_grid(stru_quad(i,3),2),stru_grid(stru_quad(i,6),3)-stru_grid(stru_quad(i,3),3),stru_grid(stru_quad(i,6),4)-stru_grid(stru_quad(i,3),4)];
    l=cross(m,n);
    l=l/(l(1)^2+l(2)^2+l(3)^2)^0.5;
    if abs(l(3))<0.1
        stru_quad_plus(end+1,:)=stru_quad(i,:);
        stru_quad(i,:)=[];
        continue
    end
    i=i+1;
end    
    
for i=1:size(stru_quad,1)
    if stru_quad(i,7)==0
        for j=1:size(stru_quad,1)
            if i==j
                continue
            end
            x1=stru_grid(stru_quad(j,3),2); x2=stru_grid(stru_quad(j,4),2); x3=stru_grid(stru_quad(j,5),2); x4=stru_grid(stru_quad(j,6),2);
            y1=stru_grid(stru_quad(j,3),3); y2=stru_grid(stru_quad(j,4),3); y3=stru_grid(stru_quad(j,5),3); y4=stru_grid(stru_quad(j,6),3);
            z1=stru_grid(stru_quad(j,3),4); z2=stru_grid(stru_quad(j,4),4); z3=stru_grid(stru_quad(j,5),4); z4=stru_grid(stru_quad(j,6),4);
            for k=3:6
                m=[x1-stru_grid(stru_quad(i,k),2), y1-stru_grid(stru_quad(i,k),3)];
                n=[x2-stru_grid(stru_quad(i,k),2), y2-stru_grid(stru_quad(i,k),3)];
                l=[x3-stru_grid(stru_quad(i,k),2), y3-stru_grid(stru_quad(i,k),3)];
                q=[x4-stru_grid(stru_quad(i,k),2), y4-stru_grid(stru_quad(i,k),3)];
                if abs(stru_grid(stru_quad(i,k),2)-stru_grid(stru_quad(j,3),2))>500||abs(stru_grid(stru_quad(i,k),3)-stru_grid(stru_quad(j,3),3))>500
                    break
                elseif stru_quad(i,k)==stru_quad(j,3)||stru_quad(i,k)==stru_quad(j,4)||stru_quad(i,k)==stru_quad(j,5)||stru_quad(i,k)==stru_quad(j,6)
                    continue
                elseif acos(dot(m,n)/(norm(m)*norm(n)))+acos(dot(n,l)/(norm(n)*norm(l)))+acos(dot(l,q)/(norm(l)*norm(q)))+acos(dot(q,m)/(norm(q)*norm(m)))>=6||...
                        norm(m)==0||norm(n)==0||norm(l)==0||norm(q)==0
                    D=[stru_grid(stru_quad(i,k),2) stru_grid(stru_quad(i,k),3) 1]/([x1 y1 1;x2 y2 1;x3 y3 1])*[z1;z2;z3]-stru_grid(stru_quad(i,k),4);
                    DD=[stru_grid(stru_quad(i,k),2) stru_grid(stru_quad(i,k),3) 1]/([x1 y1 1;x2 y2 1;x4 y4 1])*[z1;z2;z4]-stru_grid(stru_quad(i,k),4);
                    if D*DD<=0&&abs(x3-stru_grid(stru_quad(i,k),2))+abs(y3-stru_grid(stru_quad(i,k),3))>=abs(x4-stru_grid(stru_quad(i,k),2))+abs(y4-stru_grid(stru_quad(i,k),3))
                        D=DD;
                    end
                    if D<0
                        stru_quad(i,7)=1;
                        stru_quad_up(end+1)=stru_quad(i,1);
                        stru_grid_up(end+1:end+4,:)=stru_grid(stru_quad(i,3:6),:);
                        stru_quad(j,7)=2;
                        stru_quad_down(end+1)=stru_quad(j,1);
                        stru_grid_down(end+1:end+4,:)=stru_grid(stru_quad(j,3:6),:);
                    elseif D>0
                        stru_quad(i,7)=2;
                        stru_quad_down(end+1)=stru_quad(i,1);
                        stru_grid_down(end+1:end+4,:)=stru_grid(stru_quad(i,3:6),:);
                        stru_quad(j,7)=1;
                        stru_quad_up(end+1)=stru_quad(j,1);
                        stru_grid_up(end+1:end+4,:)=stru_grid(stru_quad(j,3:6),:);
                    else
                    end
                    break
                else
                    continue
                end
            end
            if stru_quad(i,7)==0
                if j==size(stru_quad,1)
                    stru_quad_undetermined(end+1,:)=stru_quad(i,:);
                end
            else 
                break
            end
        end
    else
        continue
    end
end

%% 排序，删除重复节点
stru_grid_up=sortrows(stru_grid_up,1);
stru_grid_down=sortrows(stru_grid_down,1);
i=1;
while true
    if stru_grid_up(i,1)==stru_grid_up(i+1,1)
        stru_grid_up(i+1,:)=[];
    else
        i=i+1;
    end
    if i==size(stru_grid_up,1)
        break
    end
end
i=1;
while true
    if stru_grid_down(i,1)==stru_grid_down(i+1,1)
        stru_grid_down(i+1,:)=[];
    else
        i=i+1;
    end
    if i==size(stru_grid_down,1)
        break
    end
end

%% 进行第二轮上下表面分类
i=1;
m=size(stru_quad_undetermined,1);n=m;
while true
    if m==0
        break
    end
    if i>m
        if m==n&&m~=0
            disp('Some nodes will not calculate in spline.')
            break
        else
            n=m;
        end
        i=1;
    end
    if sum(ismember(stru_quad_undetermined(i,3:6),stru_grid_up(:,1))==1)>=3
        stru_grid_up(end+1:end+4,:)=stru_grid(stru_quad_undetermined(i,3:6),:);
        stru_quad_up(end+1)=stru_quad_undetermined(i,1);
        stru_quad_undetermined(i,:)=[];
        m=m-1;
        continue
    elseif sum(ismember(stru_quad_undetermined(i,3:6),stru_grid_down(:,1))==1)>=3
        stru_grid_down(end+1:end+4,:)=stru_grid(stru_quad_undetermined(i,3:6),:);
        stru_quad_down(end+1)=stru_quad_undetermined(i,1);
        stru_quad_undetermined(i,:)=[];
        m=m-1;
        continue
    else
        i=i+1;
    end
end

%% 进行第三轮上下表面分类
stru_quad_up_plus=[];
stru_quad_down_plus=[];
stru_grid_middle=[];
for i=1:size(stru_quad_plus,1)
    if stru_grid(stru_quad_plus(i,3),3)<1500
        continue
    end
    if length(find(stru_grid_up(:,1)==stru_quad_plus(i,3)))+length(find(stru_grid_up(:,1)==stru_quad_plus(i,4)))+...
            length(find(stru_grid_up(:,1)==stru_quad_plus(i,5)))+length(find(stru_grid_up(:,1)==stru_quad_plus(i,6)))==0
        stru_quad_down_plus(end+1,:)=stru_quad_plus(i,:);
    elseif length(find(stru_grid_down(:,1)==stru_quad_plus(i,3)))+length(find(stru_grid_down(:,1)==stru_quad_plus(i,4)))+...
            length(find(stru_grid_down(:,1)==stru_quad_plus(i,5)))+length(find(stru_grid_down(:,1)==stru_quad_plus(i,6)))==0
        stru_quad_up_plus(end+1,:)=stru_quad_plus(i,:);
    else
        stru_grid_middle(end+1:end+4)=stru_grid(stru_quad_plus(i,3:6),1);
    end
end
stru_quad_up_plus(end+1,1:7)=-1;
stru_quad_down_plus(end+1,1:7)=-1;

%% 排序，删除重复节点
stru_grid_up=sortrows(stru_grid_up,1);
stru_grid_down=sortrows(stru_grid_down,1);
stru_quad_up=sort(stru_quad_up);
stru_quad_down=sort(stru_quad_down);
i=1;
while true
    if stru_grid_up(i,1)==stru_grid_up(i+1,1)
        stru_grid_up(i+1,:)=[];
    else
        i=i+1;
    end
    if i==size(stru_grid_up,1)
        break
    end
end
i=1;
while true
    if stru_grid_down(i,1)==stru_grid_down(i+1,1)
        stru_grid_down(i+1,:)=[];
    else
        i=i+1;
    end
    if i==size(stru_grid_down,1)
        break
    end
end
i=1;
while true
    if stru_quad_up(i)==stru_quad_up(i+1)
        stru_quad_up(i+1)=[];
    else
        i=i+1;
    end
    if i==length(stru_quad_up)
        break
    end
end
i=1;
while true
    if stru_quad_down(i)==stru_quad_down(i+1)
        stru_quad_down(i+1)=[];
    else
        i=i+1;
    end
    if i==length(stru_quad_down)
        break
    end
end

%% 数据整理
strubox_rigid(:,1)=stru_quad(:,1);
strubox_rigid(:,2)=stru_quad(:,3);
strubox_rigid(:,6)=stru_quad(:,4);
strubox_rigid(:,10)=stru_quad(:,5);
strubox_rigid(:,14)=stru_quad(:,6);
for i=1:size(stru_grid,1)
    for j=1:size(strubox_rigid,1)
        if stru_grid(i,1)==strubox_rigid(j,2)
            strubox_rigid(j,3:5)=stru_grid(i,2:4);
        end
        if stru_grid(i,1)==strubox_rigid(j,6)
            strubox_rigid(j,7:9)=stru_grid(i,2:4);
        end
        if stru_grid(i,1)==strubox_rigid(j,10)
            strubox_rigid(j,11:13)=stru_grid(i,2:4);
        end
        if stru_grid(i,1)==strubox_rigid(j,14)
            strubox_rigid(j,15:17)=stru_grid(i,2:4);
        end
    end
end

%% 检查分类结果
if size(strubox_rigid,1)~=length(stru_quad_up)+length(stru_quad_down)
    bad_mesh=[];
    for i=1:length(stru_quad_down)
        if ismember(stru_quad_down(i),stru_quad_up)
            bad_mesh(end+1)=stru_quad_down(i);
        end
    end
    for i=1:length(stru_quad_up)
        if ismember(stru_quad_up(i),stru_quad_down)
            bad_mesh(end+1)=stru_quad_up(i);
        end
    end
    if ~isempty(bad_mesh)
        disp('Bad mesh type 1, fatal error.')
        disp(bad_mesh)
        pause
    end
    bad_mesh=[];
    for i=1:length(strubox_rigid(:,1))
        if ismember(strubox_rigid(i,1),stru_quad_up)||ismember(strubox_rigid(i,1),stru_quad_down)
        else
            bad_mesh(end+1)=strubox_rigid(i,1);
            strubox_rigid(i,:)=[];
        end
        if i==length(strubox_rigid(:,1))
            break
        end
    end
    disp('Bad mesh tpye 2.')
    disp(bad_mesh)
end
aerobox=strubox_rigid;
end